var indexs;
var Feng = {
    ctxPath: "",
    // ctxPath: "/CRMSX",
    addCtx: function (ctx) {
        if (this.ctxPath == "") {
            this.ctxPath = ctx
        }
    }, confirm: function (tip, ensure) {
        parent.layer.confirm(tip, {btn: ['确定', '取消']}, function (index) {
            ensure();
            parent.layer.close(index)
        }, function (index) {
            parent.layer.close(index)
        })
    }, log: function (info) {
    }, alert: function (info, iconIndex) {
        parent.layer.msg(info, {icon: iconIndex})
    }, info: function (info) {
        Feng.alert(info, 0)
    }, success: function (info) {
        Feng.alert(info, 1)
    }, error: function (info) {
        Feng.alert(info, 2)
    }, infoDetail: function (title, info) {
        var display = "";
        if (typeof info == "string") {
            display = info
        } else {
            if (info instanceof Array) {
                for (var x in info) {
                    display = display + info[x] + "<br/>"
                }
            } else {
                display = info
            }
        }
        indexs = layer.open({
            type: 1,
            area: ['90%', '90%'],
            fix: true,
            maxmin: true,
            content: '<button type="button" class="btn" onclick="closesd()">关闭</button>' +
            '<div style="padding: 20px;">' + display + '</div>'
        });
        this.layerIndex = indexs;
    }, writeObj: function (obj) {
        var description = "";
        for (var i in obj) {
            var property = obj[i];
            description += i + " = " + property + ","
        }
        layer.alert(description, {skin: 'layui-layer-molv', closeBtn: 0})
    }, showInputTree: function (inputId, inputTreeContentId, leftOffset, rightOffset) {
        var onBodyDown = function (event) {
            if (!(event.target.id == "menuBtn" || event.target.id == inputTreeContentId || $(event.target).parents("#" + inputTreeContentId).length > 0)) {
                $("#" + inputTreeContentId).fadeOut("fast");
                $("body").unbind("mousedown", onBodyDown)
            }
        };
        if (leftOffset == undefined && rightOffset == undefined) {
            var inputDiv = $("#" + inputId);
            var inputDivOffset = $("#" + inputId).offset();
            $("#" + inputTreeContentId).css({
                left: inputDivOffset.left + "px",
                top: inputDivOffset.top + inputDiv.outerHeight() + "px"
            }).slideDown("fast")
        } else {
            $("#" + inputTreeContentId).css({left: leftOffset + "px", top: rightOffset + "px"}).slideDown("fast")
        }
        $("body").bind("mousedown", onBodyDown)
    }, baseAjax: function (url, tip) {
        var ajax = new $ax(Feng.ctxPath + url, function (data) {
            Feng.success(tip + "成功!")
        }, function (data) {
            Feng.error(tip + "失败!" + data.responseJSON.message + "!")
        });
        return ajax
    }, changeAjax: function (url) {
        return Feng.baseAjax(url, "修改")
    }, zTreeCheckedNodes: function (zTreeId) {
        var zTree = $.fn.zTree.getZTreeObj(zTreeId);
        var nodes = zTree.getCheckedNodes();
        var ids = "";
        for (var i = 0, l = nodes.length; i < l; i++) {
            ids += "," + nodes[i].id
        }
        return ids.substring(1)
    }, eventParseObject: function (event) {
        event = event ? event : window.event;
        var obj = event.srcElement ? event.srcElement : event.target;
        return $(obj)
    }, sessionTimeoutRegistry: function () {
        $.ajaxSetup({
            contentType: "application/x-www-form-urlencoded;charset=utf-8",
            complete: function (XMLHttpRequest, textStatus) {
                var sessionstatus = XMLHttpRequest.getResponseHeader("sessionstatus");
                if (sessionstatus == "timeout") {
                    window.location = Feng.ctxPath + "/global/sessionError"
                }
            }
        })
    }, initValidator: function (formId, fields) {
        $('#' + formId).bootstrapValidator({
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            }, fields: fields, live: 'enabled', message: '该字段不能为空'
        })
    }
};
closesd = function () {
    layer.close(indexs);
};